package gov.nist.ssd.swqb.commands;

import gov.nist.ssd.swqb.gui.ErrorDisplayer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Benjamin Herbomez
 */
public class CommandManager {

    private List<Command> commands = new ArrayList<Command>();
    private int index = -1;
    Thread t = new Thread();

    public void exec(final Command c) {
        /*if (t.isAlive()) {
            System.out.println(t.getName());
            ErrorDisplayer.getInstance().displayError("An other action is currently processing. Please whait");
            return;
        }*/

        /*t = new Thread(new Runnable() {

            @Override
            public void run() {*/
                if (index > 0) {
                    commands = commands.subList(0, index + 1);
                }
                commands.add(c);
                c.exec();
                index = commands.size() - 1;
            /*}
        });
        t.start();*/
    }

    public void undo() {
        if (index == -1) {
            return;
        }
        /*if (t.isAlive()) {
            ErrorDisplayer.getInstance().displayError("An other action is currently processing. Please whait");
            return;
        }
        t = new Thread(new Runnable() {

            @Override
            public void run() {*/
                commands.get(index--).unExec();
            /*}
        });
        t.start();*/
    }

    public void redo() {
        if (index >= commands.size() - 1) {
            return;
        }
        /*if (t.isAlive()) {
            ErrorDisplayer.getInstance().displayError("An other action is currently processing. Please whait");
            return;
        }
        t = new Thread(new Runnable() {

            @Override
            public void run() {*/
                commands.get(++index).exec();
            /*}
        });
        t.start();*/
    }
}
